package service.permission.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import common.entities.Page;
import dao.permission.PermissionDao;
import entities.permission.Permission;
import entities.permission.Role;
import entities.permission.RoleAndMenu;
import entities.permission.RoleAndPermission;
import service.permission.PermissionService;
import util.base.CurrentUser;
@Service
public  class PermissionServiceImpl implements PermissionService {
    @Autowired
    private PermissionDao permissionDao;
	@Override
	public void addPermission(Permission permisssion) {
		permissionDao.addPermission(permisssion);
	}
	@Override
	public List<Role> getRoles() {
		return permissionDao.getRoles();
	}
	@Override
	public Page<Role> getRolePage(String name, int page, int rows) {
		Page<Role> pageRole = new Page<Role>();
		List<Role> rolelist=new ArrayList<Role>();
		if(!"".equals(name)&&null!=name){
			name="%"+name+"%";
		}
		int total=permissionDao.getRoleCount(name);
		int offset=(page-1)*rows;
		rolelist=permissionDao.getRolePage(name,offset,rows);
		pageRole.setTotal(total);
		pageRole.setItems(rolelist);
		return pageRole;
	}
	@Override
	public Page<Permission> getPermissionPage(String name, int page, int rows) {
		Page<Permission> pagePermission = new Page<Permission>();
		List<Permission> permissionlist=new ArrayList<Permission>();
		if(!"".equals(name)&&null!=name){
			name="%"+name+"%";
		}
		int total=permissionDao.getPermissionCount(name);
		int offset=(page-1)*rows;
		permissionlist=permissionDao.getPermissionPage(name,offset,rows);
		pagePermission.setTotal(total);
		pagePermission.setItems(permissionlist);
		return pagePermission;
	}
	@Override
	public boolean isExistRole(String id, String name) {
		Role role=permissionDao.getRole(id,name);
		if(role==null){
			return false;
		}
		return true;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void addRole(Role role) {
		role.setId(UUID.randomUUID().toString());
		role.setAdd_person(CurrentUser.getCurrentUser().getName());
		role.setAdd_time(new Date());
		permissionDao.addRole(role);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateRole(Role role) {
		role.setUpdate_person(CurrentUser.getCurrentUser().getName());
		role.setUpdate_time(new Date());
		permissionDao.updateRole(role);
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteRoles(String ids) {
		List<String> idList=Arrays.asList(ids.split(","));
		permissionDao.deleteRoles(idList);
	}
	@Override
	public List<RoleAndMenu> getRoleAndMenu(String roleId) {
		List<RoleAndMenu> signs=permissionDao.getRoleAndMenu(roleId);
		return signs;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setMenuPermission(String roleId, List<String> checkedMenuId) {
		List<RoleAndMenu> oldSign=permissionDao.getRoleAndMenu(roleId);
		List<String> roleAndMenuIds=new ArrayList<String>();
		//新增新添的
		for(int i=0;i<checkedMenuId.size();i++){
			boolean isExist=false;
			for(int j=0;j<oldSign.size();j++){
				if(oldSign.get(j).getMenuId().equals(checkedMenuId.get(i))){
					isExist=true;
					break;
				}
			}
			if(!isExist){
				RoleAndMenu roleandMenu=new RoleAndMenu();
				roleandMenu.setId(UUID.randomUUID().toString());
				roleandMenu.setMenuId(checkedMenuId.get(i));
				roleandMenu.setRoleId(roleId);
				roleandMenu.setAdd_person(CurrentUser.getCurrentUser().getName());
				roleandMenu.setAdd_time(new Date());
				permissionDao.addRoleAndMenu(roleandMenu);
			}
		}
	    //删除取消的
		for(int i=0;i<oldSign.size();i++){
			boolean isExist=false;
			for(int j=0;j<checkedMenuId.size();j++){
				if(oldSign.get(i).getMenuId().equals(checkedMenuId.get(j))){
					isExist=true;
					break;
				}
			}
			if(!isExist){
				roleAndMenuIds.add(oldSign.get(i).getId());
			}
		}
		if(roleAndMenuIds.size()>0){
			permissionDao.deleteRoleAndMenus(roleAndMenuIds);	
		}
	}
	@Override
	public boolean isExistPermission(String id, String name) {
		Permission permission=permissionDao.getPermission(id,name);
		if(permission==null){
			return false;
		}
		return true;
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updatePermission(Permission permission) {
		permissionDao.updatePermission(permission);
	}
	@Override
	public void deletePermissions(String ids) {
		List<String> idList=Arrays.asList(ids.split(","));
		permissionDao.deletePermissions(idList);
	}
	@Override
	public List<RoleAndPermission> getRoleAndPermissions(String roleId) {
		List<RoleAndPermission> roleAndPermissions=permissionDao.getRoleAndPermissions(roleId);
		return roleAndPermissions;
	}

	@Transactional(rollbackFor = Exception.class)
	@Override
	public void setPermission(String roleId, List<String> checkedPermissionId) {
		List<RoleAndPermission> oldRoleAndPermission = permissionDao.getRoleAndPermissions(roleId);
		List<String> deleteRoleAndPermissionIds = new ArrayList<String>();

		// 新增新添的
		for (int i = 0; i < checkedPermissionId.size(); i++) {
			boolean isExist = false;
			for (int j = 0; j < oldRoleAndPermission.size(); j++) {
				if (oldRoleAndPermission.get(j).getPermissionId().equals(checkedPermissionId.get(i))) {
					isExist = true;
					break;
				}
			}
			if (!isExist) {
				RoleAndPermission roleAndPermission = new RoleAndPermission();
				roleAndPermission.setId(UUID.randomUUID().toString());
				roleAndPermission.setPermissionId(checkedPermissionId.get(i));
				roleAndPermission.setRoleId(roleId);
				roleAndPermission.setAdd_person(CurrentUser.getCurrentUser().getName());
				roleAndPermission.setAdd_time(new Date());
				permissionDao.addRoleAndPermission(roleAndPermission);
			}
		}
		// 删除取消的
		for (int i = 0; i < oldRoleAndPermission.size(); i++) {
			boolean isExist = false;
			for (int j = 0; j < checkedPermissionId.size(); j++) {
				if (oldRoleAndPermission.get(i).getPermissionId().equals(checkedPermissionId.get(j))) {
					isExist = true;
					break;
				}
			}
			if (!isExist) {
				deleteRoleAndPermissionIds.add(oldRoleAndPermission.get(i).getId());
			}
		}
		if (deleteRoleAndPermissionIds.size() > 0) {
			permissionDao.deleteRoleAndPermissions(deleteRoleAndPermissionIds);
		}
	}

}
